package com.zhipu.resetbalance.utils.serialPort;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ShellUtils;
import com.zhipu.resetbalance.listener.SerialPortListener;
import com.zhipu.resetbalance.utils.InitializeUtils;

import android_serialport_api.SerialPortFinder;
import tp.xmaihh.serialport.SerialHelper;
import tp.xmaihh.serialport.bean.ComBean;

public class WeightSerialPortUtils {
    /**
     *
     */
    private static SerialPortFinder mSerialPortFinder;
    /**
     *
     */
    private static SerialHelper mSerialHelper;
    /**
     *
     */
    private static byte[] receiveBuff = null;

    /**
     *
     */
    private static SerialPortListener serialPortListener;
    public static void setSerialPortListener(SerialPortListener mSerialPortListener) {
        serialPortListener = mSerialPortListener;
    }

    /**
     *
     */
    public static void initSerialPortOpen(){
        String sPort = "/dev/ttyS1";
        mSerialPortFinder = new SerialPortFinder();
        mSerialHelper = new SerialHelper(sPort, 9600) {
            @Override
            protected void onDataReceived(ComBean comBean) {
                if(null != comBean && null != comBean.bRec && comBean.bRec.length <= 12){
                    if(startsWith(comBean.bRec) && endsWith(comBean.bRec)){
                        if(null != serialPortListener){
                            serialPortListener.itemClicked(comBean.bRec);
                        }
                    }
                }
            }
        };
        mSerialHelper.setDataBits(8);//数据位
        mSerialHelper.setParity(0);//校验位
        mSerialHelper.setStopBits(1);//停止位
        mSerialHelper.setFlowCon(0);//流控

        try {
            mSerialHelper.open();//开启串口
            InitializeUtils.getDefaultMaker().show("串口开启成功" + mSerialHelper.getPort());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }



    /**
     * 获得所有串口设备的地址
     * @return
     */
    public static String[] getAllDeicesPath(){
        if (null == mSerialPortFinder) {
            mSerialPortFinder = new SerialPortFinder();
        }
        return mSerialPortFinder.getAllDevicesPath();
    }


    public static boolean startsWith(byte[] array) {
        if (array == null || array.length == 0) {
            return false; // 空数组不以任何字节开头
        }
        return array[0] == 2;
    }

    public static boolean endsWith(byte[] array) {
        if (array == null || array.length == 0) {
            return false; // 空数组不以任何字节结尾
        }
        return array[array.length - 1] == 4;
    }

    /**
     * 重量輸出命令
     */
    public static boolean WeightOutputCommand(){
        byte[] sendContentBytes = new byte[3];
        sendContentBytes[0] = 0x52;
        sendContentBytes[1] = 0x0D;
        sendContentBytes[2] = 0x0A;
        if(null != mSerialHelper && mSerialHelper.isOpen()){
            mSerialHelper.send(sendContentBytes);
            return true;
        }else {
            return false;
        }
    }

    /**
     * 校准500
     * @return
     */
    public static boolean Calibration500(){
        byte[] sendContentBytes = new byte[3];
        sendContentBytes[0] = 0x31;
        sendContentBytes[1] = 0x0D;
        sendContentBytes[2] = 0x0A;
        if(null != mSerialHelper && mSerialHelper.isOpen()){
            mSerialHelper.send(sendContentBytes);
            return true;
        }else {
            return false;
        }
    }

    /**
     * 綫性標定命令
     * @return
     */
    public static boolean LinearCalibrationCommand(){
        byte[] sendContentBytes = new byte[3];
        sendContentBytes[0] = 0x43;
        sendContentBytes[1] = 0x0D;
        sendContentBytes[2] = 0x0A;
        if(null != mSerialHelper && mSerialHelper.isOpen()){
            mSerialHelper.send(sendContentBytes);
            return true;
        }else {
            return false;
        }
    }


    /**
     * 取消定标
     */
    public static boolean  cancelCalibration() {
        byte[] sendContentBytes = new byte[3];
        sendContentBytes[0] = 0x1B;
        sendContentBytes[1] = 0x0D;
        sendContentBytes[2] = 0x0A;
        if(null != mSerialHelper && mSerialHelper.isOpen()){
            mSerialHelper.send(sendContentBytes);
            return true;
        }else {
            return false;
        }
    }


    /**
     * 置零命令
     */
    public static boolean ZeroOrder(){
        byte[] sendContentBytes = new byte[3];
        sendContentBytes[0] = 0x5A;
        sendContentBytes[1] = 0x0D;
        sendContentBytes[2] = 0x0A;
        if(null != mSerialHelper && mSerialHelper.isOpen()){
            mSerialHelper.send(sendContentBytes);
            return true;
        }else {
            return false;
        }
    }

}
